# -*- coding: utf-8 -*-
"""
Created on Tue Apr  3 11:07:30 2018

@author: qiang.qian
"""
import numpy as np
import seaborn as sns
import pandas as pd

from scipy.optimize import curve_fit
from matplotlib import pyplot as plt  

def func(x, a, b, c):
    return a * np.exp(-b * x) + c

df = pd.read_csv(r"d:\qian\data\user_profile\button_apply.txt",sep='\t',
            dtype={"bu":str,"uid":str,"user_name":str,"gender":str,"birthday":str,"age":float,"work_city":str,"province":str,"flight_power":float,"flight_cabin_trend":str,"hotel_power":float,"hotel_area_trend":str,"hotel_band_trend":str,"train_power":float,"train_cabin_trend":str,"credit_max_amount":float,"holiday_avg_count":float,"holiday_power":float,"holiday_city_tendency":str})

#拟合信用卡金额
df1 = df[(df['credit_max_amount'] > 0) & (df['credit_max_amount'] <= 20000)]
credit_max_amount = df1['credit_max_amount'].value_counts().sort_index()
sns.distplot(df1['credit_max_amount'],axlabel ='信用卡最大金额')
plt.show()
plt.gcf().clear() 
#credit_max_amount.plot.bar(figsize=(10, 10),fontsize=12)
xs = np.array(credit_max_amount.index.tolist())/1000
ys = np.array(credit_max_amount.values.tolist())/1000


popt, pcov = curve_fit(func, xs, ys)

plt.plot(xs, ys, 's',label='original values')
plt.plot(xs, func(xs, *popt), 'r-', label='fit')  



